#pragma once

namespace dotNetInteropPlugin {
	namespace PluginDev {
		using namespace System;
		using namespace System::Collections;
		using namespace System::Collections::Generic;

		//typedef System::Collections::ObjectModel::Collection<RootMenuItem> OptCollectionBase;
		typedef dotNetInteropPlugin::PluginDev::Additions::MBRCollection<MAIMPOptionFrame^> OptCollectionBase;

		private ref class MAIMPOptionFramesManager: public OptCollectionBase, public IMAIMPOptionFramesManager, public System::IDisposable
		{
		public:
			MAIMPOptionFramesManager(IAIMP2Extended* AIMPExt)
			{
				inAIMPExt = AIMPExt;
				inAIMPExt->AddRef();
			}

			~MAIMPOptionFramesManager()
			{
				if (inAIMPExt != NULL)
				{
					this->Clear();
					inAIMPExt->Release();
					inAIMPExt = NULL;
				}
			}

		protected:
			virtual void SetItem(Int32 index, MAIMPOptionFrame^ val) override
			{
				throw gcnew System::NotImplementedException();
			}
			virtual void InsertItem(Int32 index, MAIMPOptionFrame^ val) override
			{
				if (val->RegisterFrame(inAIMPExt))
					OptCollectionBase::InsertItem(index, val);
			}

			virtual void ClearItems() override
			{				
				for (int index = 0; index < this->Count; index++)
					this->default[index]->UnregisterFrame();
				OptCollectionBase::ClearItems();
			}
			virtual void RemoveItem(Int32 index) override
			{
				if (index > 0 && index < this->Count)
					this->default[index]->UnregisterFrame();
				OptCollectionBase::RemoveItem(index);
			}

		private:
			IAIMP2Extended* inAIMPExt;
		};

	}
}